#include "../import/head.h"
#include "protocol_disp_connect_num.h"
#include "../data_struct/head.h"
#include "../global/head.h"
#include "../macro/head.h"
#include "../msg/head.h"

namespace xos_sgsj
{

    protocol_disp_connect_num::protocol_disp_connect_num()
    {
    }

    protocol_disp_connect_num::~protocol_disp_connect_num()
    {
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    int protocol_disp_connect_num::post_connect_num( session * pSession )
    {
        int ret = 0;

        xos::i_buf * pbuf = 0;

        g_pSysData->m_os.m_pXos->create( xos::i_xos::XOS_OBJ_BUF, ( void ** ) &pbuf );

        g_pSysData->m_os.m_pSerial->pack_int16( pbuf, PROTOCOL_DISP_CONNECT_NUM );
        g_pSysData->m_os.m_pSerial->pack_int16( pbuf, g_pSysData->m_settings.m_nClientNum );

        xos::i_msg * p = 0;
        ret = g_pSysData->m_os.m_pXos->create( xos::i_xos::XOS_OBJ_MSG, ( void ** ) &p );
        p->set_int( 0, xos_package::MSG_COMPOSE_PACKAGE );
        p->set_int( 1, 0 );
        p->set_bool( 1, false );
        p->set_bool( 2, false );
        p->set_buf( 0, pbuf );
        p->set_void( 0, pSession );
        p->set_void( 1, pSession->m_pEncrypt );
        pSession->add_ref( 1 );
        g_pSysData->m_os.m_pPackage->request( p );
        pbuf = 0;
        p = 0;

        return ret;
    }

    // num( 2 )
    int protocol_disp_connect_num::proc( xos::i_buf * pBuf, session * pSession )
    {
        int ret = 0;

        if( ( 0 == ret ) && ( session::CONNECT_TYPE_UNKNOWN == pSession->m_eConnectType ) )
        {
            g_pSysData->m_pPool->m_disp_list.push_back( pSession );
            pSession->m_eConnectType = session::CONNECT_TYPE_DISP;
        }

        if( 0 == ret )
        {
            pSession->net_timeout_s = 0;
        }

        if( 0 == ret )
        {
            ret = post_connect_num( pSession );
        }

        return ret;
    }

    int protocol_disp_connect_num::post_all_disp_connect_num()
    {
        int ret = 0;

        for( session::SESSION_LIST_ITER i = g_pSysData->m_pPool->m_disp_list.begin(); i != g_pSysData->m_pPool->m_disp_list.end(); ++i )
        {
            post_connect_num( *i );
        }

        return ret;
    }
    
} // xos_sgsj
